﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace SysCommon
{
    public static class Email
    {

        public static void SendEmail(string MailRecipient, string MailSubject, string MailContent)
        {
            Email.SMTPServer = "smtp.sina.cn";
            Email.SMTPPort = 25;
            Email.E_MailAddress = "valeb@sina.cn";
            Email.E_MailPassword = "2580203./*";
            Email.SendTextEmail(MailRecipient, MailSubject, MailContent);
        }
            
        public static string SMTPServer { get; set; }
        public static int SMTPPort { get; set; }
        public static string E_MailAddress { get; set; }
        public static string E_MailPassword { get; set; }

        public static void SendTextEmail(string MailRecipient, string MailSubject, string MailContent, string fileName = null)
        {
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = SMTPServer;
            smtpClient.Port = SMTPPort;
            smtpClient.Credentials = new NetworkCredential(E_MailAddress, E_MailPassword);
            smtpClient.EnableSsl = false;

            MailAddress mailSender = new MailAddress(E_MailAddress, E_MailAddress);
            MailAddress mailFrom = new MailAddress(E_MailAddress, E_MailAddress);

            if (smtpClient != null)
            {
                string[] Mail = MailRecipient.Split(';');

                MailMessage mailMessage = new MailMessage();
                for (int i = 0; i < Mail.Length; i++)
                {
                    if (Mail.Length > 0)
                        mailMessage.To.Add(new MailAddress(Mail[i]));
                }
                mailMessage.Subject = MailSubject;
                mailMessage.SubjectEncoding = Encoding.GetEncoding("UTF-8");
                mailMessage.Sender = mailSender;
                mailMessage.From = mailFrom;
                mailMessage.Body = MailContent;
                mailMessage.IsBodyHtml = false;
                mailMessage.Priority = MailPriority.High;
                if (!string.IsNullOrEmpty(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        mailMessage.Attachments.Add(new Attachment(fs, System.Net.Mime.MediaTypeNames.Application.Octet));
                    }
                }
                smtpClient.Send(mailMessage);
            }
        }

        public static void SendHtmlEmail(string MailRecipient, string MailSubject, string MailContent, string fileName = null)
        {
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = SMTPServer;
            smtpClient.Port = SMTPPort;
            smtpClient.Credentials = new NetworkCredential(E_MailAddress, E_MailPassword);
            smtpClient.EnableSsl = false;

            MailAddress mailSender = new MailAddress(E_MailAddress, E_MailAddress);
            MailAddress mailFrom = new MailAddress(E_MailAddress, E_MailAddress);

            if (smtpClient != null)
            {
                string[] Mail = MailRecipient.Split(';');

                MailMessage mailMessage = new MailMessage();
                for (int i = 0; i < Mail.Length; i++)
                {
                    if (Mail.Length > 0)
                        mailMessage.To.Add(new MailAddress(Mail[i]));
                }
                mailMessage.Subject = MailSubject;
                mailMessage.SubjectEncoding = Encoding.UTF8;
                mailMessage.Sender = mailSender;
                mailMessage.From = mailFrom;
                mailMessage.Body = MailContent;
                mailMessage.IsBodyHtml = true;
                mailMessage.Priority = MailPriority.High;
                if (!string.IsNullOrEmpty(fileName))
                {
                    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        mailMessage.Attachments.Add(new Attachment(fs, System.Net.Mime.MediaTypeNames.Application.Octet));
                    }
                }
                smtpClient.Send(mailMessage);
            }
        }
    }
}
